package com.abu.classroom.service.impl;


import cn.hutool.core.util.StrUtil;
import com.abu.classroom.mapper.ClassroomMapper;
import com.abu.classroom.pojo.Classroom;
import com.abu.classroom.service.ClassroomService;
import com.abu.classroom.utils.PageUtils;
import com.abu.classroom.vo.QueryConditionVO;
import com.abu.common.exception.CRException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 教室资源表 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@Service
public class ClassroomServiceImpl extends ServiceImpl<ClassroomMapper, Classroom> implements ClassroomService {


    @Override
    public PageUtils queryPageByCondition(QueryConditionVO queryCondition) {
//        获取参数
        Long page = queryCondition.getPage();
        Long limit = queryCondition.getLimit();
        Integer minSize = queryCondition.getMinSize();
        Integer maxSize = queryCondition.getMaxSize();
        String searchParam = queryCondition.getSearchParam();
//        分页条件
        if (page == null) {
            page = 1L;
        }
        if (limit == null) {
            limit = 10L;
        }
        Page<Classroom> classroomPage = new Page<>(page, limit);
//        查询条件
        QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(searchParam), "classroom_name", searchParam);
        if (minSize != null && maxSize != null && minSize.equals(maxSize)) {
            queryWrapper.eq("content", minSize);
        } else {
            queryWrapper.ge(minSize != null, "content", minSize)
                    .le(maxSize != null, "content", maxSize);
        }
//        执行查询
        Page<Classroom> pageClassroomList = this.page(classroomPage, queryWrapper);
//        返回结果集
        return new PageUtils(pageClassroomList);
    }

    @Override
    public Boolean addClassroom(String building, String name, String type, Boolean status) {
//        查询教室名字是否已经存在
        Classroom classResource = this.getOne(new QueryWrapper<Classroom>().eq("classroom_name", name));
        if (classResource != null) {
            throw new CRException("教室已存在");
        }
        Classroom classroom = new Classroom();
        classroom
                .setBuilding(building)
                .setClassroomName(name)
                .setType(type)
                .setStatus(status);
        switch (type) {
            case "0":
                classroom.setContent(30);
                break;
            case "1":
                classroom.setContent(50);
                break;
            case "2":
                classroom.setContent(90);
                break;
            default:
                throw new CRException("没有该尺寸的教室，请重新选择");
        }
//       添加教室信息
        return this.save(classroom);
    }

    @Override
    public Boolean modifiedClassroomResource(Long id, String name, String type) {
        int content;
        switch (type) {
            case "0":
                content = 30;
                break;
            case "1":
                content = 50;
                break;
            case "2":
                content = 90;
                break;
            default:
                throw new CRException("没有该尺寸的教室，请重新选择");
        }
//        判断教室名是否存在
        Classroom classroom = this.getOne(new QueryWrapper<Classroom>().eq("classroom_name", name));
        if (classroom != null) {
            throw new CRException("教室名已存在，请更换教室名");
        }
//        如果教室存在就更新教室
        UpdateWrapper<Classroom> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("classroom_id", id)
                .set("classroom_name", name)
                .set("type", type)
                .set("content", content);
        return this.update(updateWrapper);
    }

    @Override
    public Classroom getClassroomById(Long id) {
        return this.getById(id);
    }

    @Override
    public Boolean modifiedClassroomStatus(Long id, Boolean status) {
        return this.update(new UpdateWrapper<Classroom>().eq("classroom_id", id).set("status", status));
    }

    @Override
    public PageUtils queryByPage(Long page, Long limit, String buildingName, String type, String key) {
//        查询条件
        QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
//        非空拼接
        if (StringUtils.isNotBlank(buildingName)) {
            queryWrapper.eq("building", buildingName);
        }
        if (StringUtils.isNotBlank(type)) {
            queryWrapper.eq("type", type);
        }
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.like("classroom_name", key);
        }
//        教室可用
        queryWrapper.eq("status", "1");
//        构造分页
        Page<Classroom> classroomPage = new Page<>(page, limit);
//        执行查询
        Page<Classroom> pageClassroomList = this.page(classroomPage, queryWrapper);
        List<Classroom> classroomListRecords = pageClassroomList.getRecords();
        if (CollectionUtils.isEmpty(classroomListRecords)) {
            throw new CRException("数据为空", 404);
        }
//        返回结果集
        return new PageUtils(pageClassroomList);
    }

}
